package com.lagou.rpc.provider;

import com.lagou.rpc.provider.server.RpcServer;
import com.lagou.rpc.provider.zookeeper.ZookeeperServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
public class ServerBootstrapApplication implements CommandLineRunner {

    static int port = 8888;
    @Autowired
    RpcServer rpcServer;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ServerBootstrapApplication.class, args);
        ZookeeperServer server = new ZookeeperServer();
        // 1.连接zookeeper集群（和美团取得联系）
        try {
            String hostName = "127.0.0.1:" + port;
            server.getConnect();
            // 2.将服务器节点注册（入住美团）
            server.register(hostName);
            // 3.业务逻辑处理
            server.business(hostName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run(String... args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                rpcServer.startServer("127.0.01", port);
            }
        }).start();
    }
}
